﻿---@diagnostic disable: trailing-space
local extension = Package:new("kfzy_general")
extension.extensionName = "kfzy" -- 注意！！！这个名字一定要和母扩展包一致，不然无法读取武将图像和配音


extension.game_modes_whitelist = { "kfzy_mode" }
extension.game_modes_blacklist = {"nos_heg_mode", "new_heg_mode" , "aaa_role_mode", "m_1v1_mode", "m_1v2_mode", "m_2v2_mode", "zombie_mode", "chaos_mode"  }

Fk:loadTranslationTable{
  ["kfzy_general"] = "克复中原",
}


local kfzy__zhugeliang = General(extension, "kfzy__zhugeliang", "shu", 3)

local kfzy__qixing = fk.CreateTriggerSkill{
  name = "kfzy__qixing",
  anim_type = "control",
  events = {fk.EventPhaseStart , fk.GameStart, fk.DamageInflicted},
  can_trigger = function(self, event, target, player, data)
    if event == fk.GameStart then 
      return player:hasSkill(self.name)
    elseif event == fk.EventPhaseStart then
      return target == player and player:hasSkill(self.name) and player.phase == Player.Start and #player:getPile("kfzy__star") > 0
    else
      return player:hasSkill(self.name) and #player:getPile("kfzy__star") > 0 and data.damageType ~= fk.NormalDamage
    end
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    if event == fk.GameStart then 
      return true
    elseif event == fk.EventPhaseStart then
      return room:askForSkillInvoke(player, self.name)
    else
      local card = room:askForCard(player, 1, 1, false, self.name, true, ".|.|.|kfzy__star", "#kfzy__qixing:"..target.id, "kfzy__star")
      if #card > 0 then
        self.cost_data = card[1]
        return true
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if event == fk.GameStart then 
      local cards = room:getCardsFromPileByRule(".|7", 7)
      if #cards > 0 then player:addToPile("kfzy__star", cards, false) end
    elseif event == fk.EventPhaseStart then
      room:askForGuanxing(player, room:getNCards(#player:getPile("kfzy__star")))
    else
      room:obtainCard(target, self.cost_data, false)
      local choice = room:askForChoice(player, {"+1", "-1"}, self.name) 
      data.damage = data.damage + tonumber(choice)
      return data.damage <= 0
    end
  end,
}
kfzy__zhugeliang:addSkill(kfzy__qixing)

local kfzy__kanpo = fk.CreateViewAsSkill{
  name = "kfzy__kanpo",
  pattern = "jink,nullification",
  card_filter = function(self, to_select, selected)
    if #selected > 0 then return false end
    local c
    if Fk:getCardById(to_select).color == Card.Red then
      c = Fk:cloneCard("jink")
    elseif Fk:getCardById(to_select).color == Card.Black then
      c = Fk:cloneCard("nullification")
    end
    return c and ((Fk.currentResponsePattern == nil and c.skill:canUse(Self, c)) or (Fk.currentResponsePattern and Exppattern:Parse(Fk.currentResponsePattern):match(c)))
  end,
  view_as = function(self, cards)
    if #cards == 0 then return nil end
    local suit = Fk:getCardById(cards[1]).suit
    local c
    if Fk:getCardById(cards[1]).color == Card.Red then
      c = Fk:cloneCard("jink")
    elseif Fk:getCardById(cards[1]).color == Card.Black then
      c = Fk:cloneCard("nullification")
    else
      return nil
    end
    c.skillName = self.name
    c:addSubcards(cards)
    return c
  end,
  before_use = function(self, player, use)
    local room = player.room
    local judge = {
      who = player,
      reason = self.name,
      pattern = use.card.name == "jink" and ".|.|heart,diamond" or ".|.|spade,club",
    }
    room:judge(judge)

    if judge.card.color == (use.card.name == "jink" and Card.Red or Card.Black) then
      if room:getCardArea(judge.card.id) == Card.DiscardPile then
        room:obtainCard(player, judge.card, false)
      end
    end
  end,
  enabled_at_response = function(self, player, response)
    return player:usedSkillTimes(self.name, Player.HistoryTurn) < 3
  end,
}
kfzy__zhugeliang:addSkill(kfzy__kanpo)


Fk:loadTranslationTable{
  ["kfzy__zhugeliang"] = "诸葛亮",
  ["kfzy__qixing"] = "七星",
  [":kfzy__qixing"] = "游戏开始时，你将牌堆中七张点数为7的牌扣置武将牌上称为“星”。准备阶段开始时，你可以观看牌堆顶X张牌（X为“星”的数量）并以任意顺序置于牌堆顶、牌堆底。一名角色受到属性伤害时，你可以将一张“星”交给其，令此伤害+1或-1。",
  ["#kfzy__qixing"] = "七星：你可以将一张“星”交给 %src，令此伤害+1或-1",
  ["kfzy__star"] = "星",

  ["kfzy__kanpo"] = "看破",
  [":kfzy__kanpo"] = "每轮限三次，你可以将一张红色/黑色牌当【闪】/【无懈可击】使用并进行一次判定，若判定结果为红色/黑色，你获得判定牌。",

  ["$kfzy__qixing1"] = "此身抱薪，可付丹鼎，五十四年春秋昭炎汉长明",
  ["$kfzy__qixing2"] = "南征北伐，誓还旧都，二十四代王业不偏安一隅",
  ["$kfzy__qixing3"] = "三顾之谊铭心，隆中之言在耳，请托臣讨贼兴复之效",
  ["$kfzy__qixing4"] = "著大义于四海，揽天下之弼士，诚如是，则汉室可兴",
  ["$kfzy__kanpo1"] = "平二川，定三足，恍惚草堂梦里，挥斥千古风流",
  ["$kfzy__kanpo2"] = "战群儒，守空城，今摆乱石八阵，笑谈将军死生",
  
  ["~kfzy__zhugeliang"] = "一别隆中三十载，归来犹唱梁甫吟",

}

local kfzy__jiangwei = General(extension, "kfzy__jiangwei", "shu", 4)

local kfzy__tiaoxin = fk.CreateActiveSkill{
  name = "kfzy__tiaoxin",
  anim_type = "offensive",
  card_num = 0,
  card_filter = function() return false end,
  min_target_num = 1,
  max_target_num = function() return Self:getMark("kfzy__tiaoxin_times") end,
  target_filter = function(self, to_select, selected)
    return to_select ~= Self.id and #selected < self:getMaxTargetNum(Self) 
  end,
  can_use = function(self, player)
    return player:usedSkillTimes(self.name, Player.HistoryPhase) == 0 
  end,
  on_use = function(self, room, effect)
    local player = room:getPlayerById(effect.from)
    local target = room:getPlayerById(effect.tos[1])
    local choice = "#kfzy__tiaoxin_slash:"..player.id
    if not target:isNude() then
      choice = room:askForChoice(target, {"#kfzy__tiaoxin_throw:"..player.id , choice}, self.name)
    end
    if choice:startsWith("#kfzy__tiaoxin_slash") then
      room:useVirtualCard("slash", nil, target, player, self.name, true) -- 会自动判断是否合法
      room:addPlayerMark(player, "kfzy__tiaoxin_slash-phase")
    elseif not target:isNude() then
      local cid = room:askForCardChosen(player, target, "he", self.name)
      room:throwCard({cid}, self.name, target, player)
    end
  end,
}
local kfzy__tiaoxin_ts = fk.CreateTriggerSkill{ -- cilent room不能gattag还得自己手动上标记
  name = "#kfzy__tiaoxin_ts",
  refresh_events = {fk.RoundStart, fk.EventAcquireSkill},
  can_refresh = function(self, event, target, player, data)
    if event == fk.RoundStart then
      return player:hasSkill(self.name, true)
    else
      return data == self and target == player
    end
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    room:setPlayerMark(player, "kfzy__tiaoxin_times", room:getTag("RoundCount"))
  end,
}
kfzy__tiaoxin:addRelatedSkill(kfzy__tiaoxin_ts)
local kfzy__tiaoxin_tm = fk.CreateTargetModSkill{
  name = "#kfzy__tiaoxin_tm",
  residue_func = function(self, player, skill, scope, card)
    if skill and scope and skill.trueName == "slash_skill" and scope == Player.HistoryPhase  then
      return player:getMark("kfzy__tiaoxin_slash-phase")
    end
  end,
}
kfzy__tiaoxin:addRelatedSkill(kfzy__tiaoxin_tm)
kfzy__jiangwei:addSkill(kfzy__tiaoxin)

local kfzy__jiufa = fk.CreateViewAsSkill{
  name = "kfzy__jiufa",
  pattern = "fire__slash,jink",
  mute = true,
  interaction = function()
    local names = {}
    for _, name in ipairs({"fire__slash","jink"}) do
      local card = Fk:cloneCard(name)
      if (Fk.currentResponsePattern == nil and Self:canUse(card)) or
        (Fk.currentResponsePattern and Exppattern:Parse(Fk.currentResponsePattern):match(card)) then
        table.insertIfNeed(names, card.name)
      end
    end
    if #names == 0 then return end
    return UI.ComboBox {choices = names}
  end,
  card_num = 0,
  card_filter = function() return false end,
  view_as = function(self)
    if not self.interaction.data then return end
    local card = Fk:cloneCard(self.interaction.data)
    card.skillName = self.name
    return card
  end,
  before_use = function(self, player, use)
    local room = player.room
    room:removePlayerMark(player, "@kfzy__jiufa", 3)
    player:broadcastSkillInvoke(self.name, math.random(5,6))
    room:notifySkillInvoked(player, self.name, "offensive")
  end,
  enabled_at_play = function(self, player)
    return player:canUse(Fk:cloneCard("fire__slash")) and player:getMark("@kfzy__jiufa") >= 3
  end,
  enabled_at_response = function(self, player, response)
    return player:getMark("@kfzy__jiufa") >= 3 and not response
  end,
}

local kfzy__jiufa_ts = fk.CreateTriggerSkill {
  name = "#kfzy__jiufa_ts",
  mute = true,
  events = {fk.AfterCardsMove},
  can_trigger = function(self, _, _, player, data)
    if player:hasSkill(self.name) then
      for _, move in ipairs(data) do
        if move.toArea == Card.DiscardPile then
          for _, info in ipairs(move.moveInfo) do
            local card = Fk:getCardById(info.cardId)
            if card.type == Card.TypeBasic then
              return (player.phase ~= Player.NotActive and player:getMark("@kfzy__jiufa") < 9) or
              (player.phase == Player.NotActive and player:getMark("@kfzy__jiufa") > 0)
            end
          end
        end
      end
    end
  end,
  on_cost = function() return true end,
  on_use = function(_, _, _, player, data)
    local room = player.room
    local x = 0
    for _, move in ipairs(data) do
      if move.toArea == Card.DiscardPile then
        for _, info in ipairs(move.moveInfo) do
          local card = Fk:getCardById(info.cardId)
          if card.type == Card.TypeBasic then
            x = x + 1
          end
        end
      end
    end
    if player.phase ~= Player.NotActive then
      player:broadcastSkillInvoke("kfzy__jiufa", math.random(1,2))
      room:doAnimate("InvokeSkill", { name = "kfzy__jiufa", player = player.id, skill_type = "control", })
      room:addPlayerMark(player, "@kfzy__jiufa", x)
    else
      player:broadcastSkillInvoke("kfzy__jiufa", math.random(3,4))
      room:doAnimate("InvokeSkill", { name = "kfzy__jiufa", player = player.id, skill_type = "negative", })
      room:removePlayerMark(player, "@kfzy__jiufa", x)
    end
  end,
}
kfzy__jiufa:addRelatedSkill(kfzy__jiufa_ts)
kfzy__jiangwei:addSkill(kfzy__jiufa)

Fk:loadTranslationTable{
  ["kfzy__jiangwei"] = "姜维",
  ["kfzy__tiaoxin"] = "挑衅",
  [":kfzy__tiaoxin"] = "出牌阶段限一次，你可以令至多X名其他角色（X为游戏轮数）依次选一项：<br>"..
  "①你弃置其一张牌。<br>"..
  "②视为对你使用一张【杀】，本阶段你使用【杀】的次数+1。",
  ["#kfzy__tiaoxin_throw"] = "%src 弃置其一张牌",
  ["#kfzy__tiaoxin_slash"] = "视为对 %src 使用一张【杀】",

  ["kfzy__jiufa"] = "九伐",
  [":kfzy__jiufa"] = "你的回合内/回合外，每当一张基本牌进入弃牌堆后，你获得/失去1枚“九伐”标记（上限9枚）。你可以弃3枚“九伐”标记视为使用火【杀】或【闪】。",
  ["@kfzy__jiufa"] = "九伐",
  ["#kfzy__jiufa_ts"] = "九伐",

  ["$kfzy__tiaoxin1"] = "汝等小儿，可敢杀我？",
  ["$kfzy__tiaoxin2"] = "贼将早降，可免一死。",
  ["$kfzy__tiaoxin3"] = "会闻用师，观衅而动。",
  ["$kfzy__tiaoxin4"] = "宜乘其衅会，以挑敌将。",
  ["$kfzy__tiaoxin5"] = "跪下受降，饶你不死！",
  ["$kfzy__tiaoxin6"] = "黄口小儿，可听过将军名号？",

  ["~kfzy__jiangwei"] = "我计不成，乃天命也……",
}

local kfzy__dongyun = General(extension, "kfzy__dongyun", "shu", 3)

local kfzy__bingzheng = fk.CreateActiveSkill{
  name = "kfzy__bingzheng",
  anim_type = "control",
  can_use = function(self, player)
    return player:usedSkillTimes(self.name, Player.HistoryPhase) == 0
  end,
  card_num = 0,
  card_filter = function() return false end,
  max_target_num = 2,
  min_target_num = 1,
  target_filter = function(self, to_select, selected)
    local to = Fk:currentRoom():getPlayerById(to_select)
    return #selected < 2 and to:getHandcardNum() ~= to.hp
  end,
  on_use = function(self, room, effect) 
    local from,to
    for _, pid in ipairs(effect.tos) do
      local p = room:getPlayerById(pid)
      if not p.dead then 
        local x = p:getHandcardNum() - p.hp
        if x > 0 then
          room:askForDiscard(p, x, x, false, self.name, false)
          to = p
        else
          p:drawCards(-x, self.name)
          from = p
        end
      end
    end
    if from and to and not(from.dead or to.dead) then
      room:useVirtualCard("fire_attack", nil, from, to, self.name)
    end
  end,
}
kfzy__dongyun:addSkill(kfzy__bingzheng)


local kfzy__sheyan = fk.CreateTriggerSkill{
  name = "kfzy__sheyan",
  anim_type = "defensive",
  events = {fk.TargetConfirming},
  frequency = Skill.Compulsory,
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(self.name) and data.card.type == Card.TypeTrick and player:usedSkillTimes(self.name, Player.HistoryTurn) == 0
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local promot = "#kfzy__sheyan_nouser:::"..data.card:toLogString()
    if not room:getPlayerById(data.from).dead then promot = "#kfzy__sheyan::"..data.from..":"..data.card:toLogString() end
    return room:askForSkillInvoke(player, self.name, nil, promot)
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    player:drawCards(1, self.name)
    AimGroup:cancelTarget(data, player.id)
    local from = room:getPlayerById(data.from)
    if not from.dead then
      room:moveCards{
        ids = room:getSubcardsByRule(data.card, { Card.Processing }),
        toArea = Card.PlayerHand,
        moveReason = fk.ReasonPrey,
        to = data.from,
      }
    end
  end,
}

kfzy__dongyun:addSkill(kfzy__sheyan)

Fk:loadTranslationTable{
  ["kfzy__dongyun"] = "董允",
  ["kfzy__bingzheng"] = "秉正",
  [":kfzy__bingzheng"] = "出牌阶段限一次，你可以令至多两名角色将手牌数不等于体力值的角色将手牌数调整至体力值，然后以此法摸牌的角色视为对以此法弃牌的角色使用了一张【火攻】。",
  ["kfzy__sheyan"] = "舍宴",
  [":kfzy__sheyan"] = "每回合限一次，你成为锦囊牌的目标时，你可以摸一张牌并取消之，然后令使用者获得其使用的牌。",
  ["#kfzy__sheyan_nouser"] = "你可以摸一张牌并令 %arg 的目标取消你",
  ["#kfzy__sheyan"] = "你可以摸一张牌并令 %arg 的目标取消你，然后 %dest 收回 %arg",
  
  ["$kfzy__bingzheng1"] = "自古，就是邪不胜正！",
  ["$kfzy__bingzheng2"] = "主公面前，岂容小人搬弄是非！",
  ["$kfzy__sheyan1"] = "公事为重，宴席不去也罢。",
  ["$kfzy__sheyan2"] = "还是改日吧。",
  ["~kfzy__dongyun"] = "大汉，要亡于宦官之手了……",
}
















local kfzy__dengai = General(extension, "kfzy__dengai", "wei", 4)

local kfzy__tuntian = fk.CreateTriggerSkill{
  name = "kfzy__tuntian",
  anim_type = "drawcard",
  events = {fk.GameStart , fk.AfterCardsMove},
  can_trigger = function(self, event, target, player, data)
    if event == fk.GameStart then 
      return player:hasSkill(self.name)
    else
      if player:hasSkill(self.name) then
        for _, move in ipairs(data) do
          if move.from == player.id and move.extra_data and move.extra_data.kfzy__tuntian then
            return true
          end
        end
      end
    end
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    if event == fk.GameStart then 
      return true
    else
      return room:askForSkillInvoke(player, self.name)
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if event == fk.GameStart then 
      local nonbasic = table.filter(player:getCardIds("he"), function (id) return Fk:getCardById(id).type ~= Card.TypeBasic end)
      if #nonbasic > 0 then
        room:recastCard(nonbasic, player, self.name)
      end
      for _, cid in ipairs(player:getCardIds("h")) do
        if Fk:getCardById(cid).type == Card.TypeBasic then
          room:setCardMark(Fk:getCardById(cid) , "@@kfzy__field-inhand", 1)
        end
      end
    else
      local judge = {
        who = player,
        reason = self.name,
        pattern = ".|.|.|.|.|basic",
      }
      room:judge(judge)
      local card = judge.card
      if card.type == Card.TypeBasic then
        if room:getCardArea(judge.card.id) == Card.DiscardPile then
          room:obtainCard(player, judge.card, false)
        end
        if room:getCardArea(card.id) == Card.PlayerHand and room:getCardOwner(card.id) == player then
          room:setCardMark(card, "@@kfzy__field-inhand", 1)
        end
      end
    end
  end,

  refresh_events = {fk.BeforeCardsMove},
  can_refresh = function(self, event, target, player, data)
    if player:hasSkill(self.name) then
      for _, move in ipairs(data) do
        if move.from == player.id then
          for _, info in ipairs(move.moveInfo) do
            if info.fromArea == Card.PlayerHand and (Fk:getCardById(info.cardId):getMark("@@kfzy__field-inhand") > 0 
            or player.phase == Player.NotActive)then
              return true
            end
          end
        end
      end
    end
  end,
  on_refresh = function(self, event, target, player, data)
    for _, move in ipairs(data) do
      if move.from == player.id then
        for _, info in ipairs(move.moveInfo) do
          if info.fromArea == Card.PlayerHand and (Fk:getCardById(info.cardId):getMark("@@kfzy__field-inhand") > 0 
            or player.phase == Player.NotActive)then
            move.extra_data = move.extra_data or {}
            move.extra_data.kfzy__tuntian = true break
          end
        end
      end
    end
  end,
}
local kfzy__tuntian_maxcards = fk.CreateMaxCardsSkill{
  name = "#kfzy__tuntian_maxcards",
  exclude_from = function(self, player, card)
    return card:getMark("@@kfzy__field-inhand") > 0
  end,
}
kfzy__tuntian:addRelatedSkill(kfzy__tuntian_maxcards)
kfzy__dengai:addSkill(kfzy__tuntian)


local kfzy__jixi = fk.CreateViewAsSkill{
  name = "kfzy__jixi",
  anim_type = "control",
  pattern = "snatch",
  card_filter = function(self, to_select, selected)
    return Fk:getCardById(to_select):getMark("@@kfzy__field-inhand") > 0 and #selected == 0
  end,
  view_as = function(self, cards)
    if #cards ~= 1 then return nil end
    local c = Fk:cloneCard("snatch")
    c.skillName = self.name
    c:addSubcards(cards)
    return c
  end,
}
local kfzy__jixi_prohibit = fk.CreateProhibitSkill{
  name = "#kfzy__jixi_prohibit",
  is_prohibited = function(self, from, to, card)
    return from:distanceTo(to) <= 1 and card and card.name == "snatch" and table.contains(card.skillNames, "kfzy__jixi")
  end,
}
kfzy__jixi:addRelatedSkill(kfzy__jixi_prohibit)
local hasNoField = function (player)
  for _, cid in ipairs(player:getCardIds("h")) do
    local card = Fk:getCardById(cid)
    if card:getMark("@@kfzy__field-inhand") > 0 then return false end
  end
  return true
end
local kfzy__jixi_tm = fk.CreateTargetModSkill{
  name = "#kfzy__jixi_tm",
  bypass_distances = function(self, player, skill, card, to)
    return player:hasSkill("kfzy__jixi") and (table.contains(card.skillNames, "kfzy__jixi") 
    or (card and card.trueName == "slash" and to:isKongcheng() and hasNoField(player) ) )
  end,
}
kfzy__jixi:addRelatedSkill(kfzy__jixi_tm)
local kfzy__jixi_ts = fk.CreateTriggerSkill{
  name = "#kfzy__jixi_ts",
  events = {fk.DamageCaused},
  mute = true,
  can_trigger = function(_, _, target, player, data)
    return target == player and player:hasSkill("kfzy__jixi") and hasNoField(player)
     and data.to:isKongcheng() and data.card and data.card.trueName == "slash" 
  end,
  on_cost = function() return true end,
  on_use = function(_, _, _, player, data)
    player:broadcastSkillInvoke("kfzy__jixi")
    player.room:notifySkillInvoked(player, "kfzy__jixi", "offensive")
    data.damage = data.damage + 1
  end,
}
kfzy__jixi:addRelatedSkill(kfzy__jixi_ts)

kfzy__dengai:addSkill(kfzy__jixi)




Fk:loadTranslationTable{
  ["kfzy__dengai"] = "邓艾",
  ["kfzy__tuntian"] = "屯田",
  [":kfzy__tuntian"] = "游戏开始时，你重铸所有非基本牌，然后将手牌中基本牌称为“田”，“田”不计入手牌上限。当你失去“田”或回合外失去牌时，你可以进行一次判定，若结果为基本牌，获得之并称为“田”。",
  ["@@kfzy__field-inhand"] = "田",

  ["kfzy__jixi"] = "急袭",
  [":kfzy__jixi"] = "你可以将一张“田”对你距离大于1的其他角色当【顺手牵羊】使用。若你没有“田”，你对无手牌的角色使用【杀】无距离限制且伤害+1。",

  ["$kfzy__tuntian1"] = "休养生息，备战待敌。",
  ["$kfzy__tuntian2"] = "锄禾日当午，汗滴禾下土。",
  ["$kfzy__tuntian3"] = "兵农一体，以屯养战。",
  ["$kfzy__tuntian4"] = "垦田南山，志在西川。",
  ["$kfzy__tuntian5"] = "留得良田在，何愁不破敌？",
  ["$kfzy__tuntian6"] = "击鼓于此，以致四方。",

  ["$kfzy__jixi1"] = "偷渡阴平，直取蜀汉！",
  ["$kfzy__jixi2"] = "攻其无备，出其不意！",
  ["$kfzy__jixi3"] = "屯田日久，当建奇功！",
  ["$kfzy__jixi4"] = "开辟险路，奇袭敌军！",

  ["~kfzy__dengai"] = "吾破蜀克敌，竟葬于奸贼之手……",
}






Fk:loadTranslationTable{
  [""] = "",
  [""] = "",
  [""] = "",
  [""] = "",
  [""] = "",
  [""] = "",
  [""] = "",
  [""] = "",
  [""] = "",
}





return extension




